




var wxsession = require('../../template/user/tem')
const app = getApp();
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    nickName: '',
    avatarUrl:'',
    userinfo:'',   //用户信息
  },

  onLoad: function () {
    // 查看是否授权
    wx.getSetting({
      success: function(res){
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
              //用户已经授权过
            }
          })
        }
      }
    })
  },
  toprefer:function(){
    wx.navigateTo({
      url: "/pages/general/perfectInformation/perfectInformation"
    })
  },
  
  // bindGetUserInfo: function(e) {
  //   console.log(e)




  //   if (e.detail.userInfo){
  //     //用户按了允许授权按钮
  //   } else {
  //     //用户按了拒绝按钮
  //   }
  // },
  
  bindGetUserInfo: function (e) {   
    var that = this;
    that.setData({
      encryptedData: e.detail.encryptedData,
      iv:e.detail.iv
    });
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.setData({
          nowcode: res.code
        });
            wx.request({
      // 请求用户地址列表
      url:  getApp().globalData.baseUrl+'/wago/user/reg', 
      method:'post',
      data:{
        encryptedData:that.data.encryptedData,
        iv:that.data.iv,
        code:that.data.nowcode
      },
      success (res) {
         getApp().globalData.openid = res.data.data.openid
        //  console.log('9999999999999999999999999999999',res.data.data.openid)
        
        // console.log('res.data.data.flag',res.data.data.flag)
        getApp().globalData.attention = JSON.stringify(res.data.data.flag) 
        console.log('getApp().globalData.attention',getApp().globalData.attention)
        if(res.data.data.messg){
          that.toprefer()
        }
        wx.showToast({
          title: '登录成功',
          duration: 2000,
          icon: 'none'
        });
        
        // console.log('66666666', typeof getApp().globalData.attention)
          
      }
    })
      }
    })

    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      that.setData({
        userinfo:e.detail.userInfo
      })
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      wxsession.creatwx(e.detail.userInfo).then(function (e) {


        })

    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
})